package com.akuhome.resource.service.check;

import com.akuhome.resource.DO.check.AccCheck;
import com.akuhome.resource.DO.check.use.DormitoryUse;
import com.akuhome.resource.dao.CheckDao;
import com.akuhome.resource.dto.use.DormitoryUseProject;
import com.akuhome.resource.entity.Dormitory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class AccCheckService {
    @Autowired
    CheckDao checkDao;

    public DormitoryUseProject findDormitoryUseProject(Integer cid){
        return checkDao.findDormitoryUseProjectByProjectId(cid);
    }

    @Transactional(rollbackFor = {RuntimeException.class,Exception.class, SQLException.class})
    public void updateDormitoryUsage(List<DormitoryUse> dormitoryUseList){
        //取第一个项目作为参照
        DormitoryUse simple = dormitoryUseList.get(0);
        List<DormitoryUse> oldList = checkDao.findDormitoryUseByAccCheckId(simple.getAccCheckId());
        List<DormitoryUse> deleteList = new ArrayList<>(oldList);
        List<DormitoryUse> insertList = dormitoryUseList;
        deleteList.removeAll(insertList);
        insertList.removeAll(oldList);
        if(insertList.size()>0)
        checkDao.insertDormitoryUseList(insertList);
        if(deleteList.size()>0)
        checkDao.deleteDormitoryUseList(deleteList);
    }

    public Integer insertAccCheck(AccCheck accCheck,String uid){
        checkDao.insertAccCheck(accCheck,uid);
        return accCheck.getId();
    }

    public void insertDormitoryUseList(List<DormitoryUse> dormitoryUseList){
        checkDao.insertDormitoryUseList(dormitoryUseList);
    }
}
